package com.spring.bean.customtag.use;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

/**
 * @ClassName: UserBeanDefinitionParser
 * @Author: suncx
 * @CreateDate: 2020-03-26 13:22
 * @Description:
 */
public class UserBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
    //Element 对应的类
    @Override
    protected Class getBeanClass (Element element) {
        return User.class;
    }
        //从 element 中解析提取对应元素
    @Override
        protected void doParse (Element element , BeanDefinitionBuilder bean ) {
            String userName = element.getAttribute("userName");
            String email = element.getAttribute("email");
            //将提取出来的数据放在BeanDefinitionBuilder 中，待完全解析后放在统一注册到beanFactory中
            if (StringUtils.hasText(userName)) {
                bean.addPropertyValue("userName", userName);
                if (StringUtils.hasText(email)) {
                    bean.addPropertyValue("email", email);
                }
            }
        }
}
